<template>
	<view class="container">
		<view class="title">
			<!--
			<view class="flex f-a-c flex-1">
				<view class="flex flex-1 h-32  b-radius-30 bg-color-f9 padding-lr4">
					<text class="flex f-a-c f-s-0 van-icon van-icon-search padding-lr2 f18-size t-color-0 margin-r6"></text>
					<view class="flex flex-1 h100">
						<input  type="text" value="" class="input f12-size" :placeholder="搜索商品" />
					</view>
					<view class="flex f-a-c h100">
						<view @submit="search" class="flex f-a-c f-j-c f-s-0 w-50 b-radius-30 h-26 bg-color-linear-y t-color-w f12-size">搜索</view>
					</view>
				</view>
			</view> -->

			<view class="search">
				<view class="title_search">

					<form @submit="search" report-submit="true">

						<!-- <input  type="text" :name="where.keyword" v-model="where.keyword" placeholder="搜索商品" confirm-type='search'/> -->
						<view class="flex f-a-c flex-1">
							<view class="flex flex-1 h-32  b-radius-30 bg-color-f9 padding-lr4">
								<text class="flex f-a-c f-s-0 van-icon van-icon-search padding-lr2 f18-size t-color-0 margin-r6"></text>
								<view class="flex flex-1 h100">
									<input  type="text" v-model="where.keyword" class="input f12-size" placeholder="搜索商品" />
								</view>
								<view class="flex f-a-c h100">
									<view @click="search" class="flex f-a-c f-j-c f-s-0 w-50 b-radius-30 h-26 bg-color-linear-y t-color-w f12-size">搜索</view>
								</view>
							</view>
						</view>
					</form>
				</view>
			</view>
			<view class="longTab">
				<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation class="menu"  scroll-into-view="category_id-1" scroll-left="111" show-scrollbar="false">
					<view
						class="longItem"
						:style="'width:' + isWidth + 'px'"
						:data-index="index"

						v-for="(item, index) in menuList"
						:key="index"
						:id="'category_id-' + index"

						@click="selectMenu(item, index)"
					>
						{{ item.name }}
					</view>
					<view v-if="menuList.length" class="underlineBox" :style="'transform:translateX(' + isLeft + 'px);width:' + isWidth + 'px'">
						<view class="underline bg-color-white"></view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view v-if="shopList.length">
			<block v-for="(item, index) in shopList" :key="index">
				<view class="shop_list">
					<view class="shop_list_item">
						<view class="shop_list_item_shop">
							<view class="shop_list_item_shop_image">
								<image :src="item.mainImg"></image>
								<text v-if="item.specType == 1" class="spec">多规格</text>
							</view>
							<view class="shop_list_item_shop_con">
								<view class="shop_list_item_shop_con_title line1">{{ item.title }}</view>
								<view class="shop_list_item_shop_con_message">
									<span>库存：{{ item.stock }}</span>
									<span>销量：{{ item.sellNum }}</span>
								</view>
								<view class="shop_list_item_shop_con_price">
									<span>￥{{ item.price }}</span>
									<del v-if="item.oriPrice">￥{{ item.oriPrice }}</del>
								</view>
							</view>
						</view>
						<view class="shop_list_item_handle">
							<view v-if="item.status == 1 && stype != 5" class="shop_button" @click="upperShelfAndLowerShelf(item, index)">下架</view>
							<view v-if="item.status == 2 && stype != 5" class="shop_button" @click="upperShelfAndLowerShelf(item, index)">上架</view>
							<view class="shop_button" v-if="stype != 1 && stype != 3" @click="handleDelete(item, index)">删除</view>
							<view class="shop_button" v-if="stype == 5" @click="reduction(item, index)">恢复</view>
							<navigator :url="`/pages/product/addGoods/index?mer_id=${merId}&product_id=${item.id}`" v-if="stype != 5" class="shop_button" hover-class="none">编辑</navigator>
						</view>
					</view>
				</view>
			</block>
		</view>
		<Loading :loaded="loaded" :loading="loading"></Loading>
	</view>
</template>

<script>

import { productLstApi, productTitle, productDeleteApi, productDeleteApiJoin, productOffApi, productOnApi, productOffApiJoin, productOnApiJoin, productRestore, productDestory } from '@/utils/api/admin.js';

;

import { Toast, Modal, navigateTo,getStorage,  removeStorage } from '@/utils/libs/uniApi.js';

export default {
	components: {
	},
	data() {
		return {
				scroll_category_id: 'scroll_category_id_0',
			stype: 1,
			tabClick: 0,
			tabLeft: 0,
			isLeft: 0, //导航栏下划线位置
			isWidth: 0, //每个导航栏占位
			childIndex: 0,
			// menuList: [],
			merId: '',
			shopList: [],
			loaded: false,
			loading: false,
			where: {
				page: 1,
				limit: 15,
				keyword: '',
			},

			canNotReade: true,
			menuList : [
					{
					'stype' : 1,
					'name' : '出售中' ,
					'count' : 1
					},
					{
					'stype' : 2,
					'name' : '仓库中' ,
					'count' : 1
					},
					{
					'stype' : 3,
					'name' : '已售罄' ,
					'count' : 1
					},
					// {
					// 'stype' : 4,
					// 'name' : '警戒库存',
					// 'count' :1,
					// },
					// {
					// 'stype' : 5,
					// 'name' : '回收站' ,
					// 'count' :1
					// },
					// {
					// 'stype' :6,
					// 'name' : '待审核' ,
					// 'count' : 1
					// },
					// {
					// 'stype':7,
					// 'name':'审核未通过' ,
					// 'count' :1
					// }
			]
		};
	},
	created() {
		var that = this;
		// 获取设备宽度
		uni.getSystemInfo({
			success(e) {
				that.isWidth = e.windowWidth / 5;


				that.isLeft = (that.stype-1) * that.isWidth; //设置下划线位置

			}
		});
	},

	onLoad(opt) {
		this.merId = opt.mer_id;
		this.$set(this, 'stype', opt.stype ? Number(opt.stype) : 1);
		this.getProductTitle();
		this.initData();
	},
	onShow() {},
	// 下拉到底部
	onReachBottom() {
		this.initData();
	},
	methods: {
		initData() {
			var that = this;
			if (that.loading || that.loaded) return;
			that.loading = true;
			productLstApi(this.merId, { ...this.where, stype: this.stype }).then(
				res => {
					that.loading = false;
					that.loaded = res.data.list.length < that.where.limit;
					that.shopList.push.apply(that.shopList, res.data.list);
					that.where.page = that.where.page + 1;
				},
				error => {
					that.Tips({
						title: error.msg
					});
				}
			);
		},
		search(){
			let that = this;
			that.loading = that.loaded = false;
			that.where.page = 1;
			that.shopList = [];
			
			console.log(that.where)
			that.initData();
		},
		// 获取列表头部
		getProductTitle() {
			let _this = this
			// productTitle(this.merId).then(res => {



				// this.menuList = res.data;
				let clickIndex = this.stype;
				this.menuList.forEach((item, index) => {
					if (item.stype == this.stype) {
						clickIndex = index;
					}
				});

				this.setMenuLeft(clickIndex);


				// if(this.stype!=1){
				// 	setTimeout(function(){
				// 		console.log("111")
				// 		_this.isLeft = (_this.stype-1) * _this.isWidth; //设置下划线位置
				// 		console.log(" this.isLeft", _this.isLeft)
				// 		console.log(" this.isWidth", _this.isWidth)
				// 	},500)


				// }
				// console.log(this.menuList);
			// });
		},
		// 点击列表头部
		selectMenu(item, index) {
			this.stype = item.stype;
			this.loading = this.loaded = false;
			this.where.page = 1;
			this.shopList = [];
			this.setMenuLeft(index);
		},
		/*导航栏移动位置*/
		setMenuLeft(index) {
			this.childIndex = 0;
			if (this.menuList.length > 5) {
				var tempIndex = index - 2;
				tempIndex = tempIndex <= 0 ? 0 : tempIndex;
				let tabLeft = (index - 2) * this.isWidth; //设置下划线位置
				this.$nextTick(function() {
					this.$set(this, 'tabLeft', tabLeft);
				});
			}
			this.tabClick = index; //设置导航点击了哪一个
			this.isLeft = index * this.isWidth; //设置下划线位置

			this.initData();
		},
		//还原
		reduction(item, index) {
			Modal('温馨提示', `商品"${item.title}"，将被还原，请问是否继续`).then(() => {
				productRestore(this.merId, item.product_id).then(res => {
					this.Tips({ title: res.message, icon: 'success' });
					this.shopList.splice(index, 1);
				});
			});
		},
		editGoods(item) {
			console.log(item);
			let waitDeleteData = [
				'addGoodsFormData',
				'singleSpecification',
				'attrValue',
				'modifyPriceData',
				'addGoodssecondData',
				'goodsDis',
				'editGoodsDetils',
				'canChange',
				'canChangesecond'
			];
			waitDeleteData.forEach(item => {
				if (getStorage(item)) {
					removeStorage(item);
				}
			});
			navigateTo(1, '/pages/product/addGoods/index', { mer_id: this.merId, type: 'edit', product_id: item.product_id, type: 'edit' });
		},
		// 加入回收站
		handleDelete(item, index) {
			Modal('温馨提示', `商品"${item.title}"将被删除，请问是否继续?`).then(res => {
					productDeleteApiJoin(this.merId, item.id)
						.then(res => {
							this.shopList.splice(index, 1);
							Toast('删除成功');
						})
						.catch(rej => {
							Toast(`${rej}`);
						});
			  })
		},
		// 上架下架
		upperShelfAndLowerShelf(obj, index) {
			let status = obj.status == 1 ? 0 : 1;
			if(status==0){
				productOffApiJoin(this.merId, obj.id)
					.then(res => {
						this.Tips({ title: res.message, icon: 'success' });
						this.shopList.splice(index, 1);
					})
					.catch(err => {
						return this.Tips({
							title: err
						});
					});
			}else{
				productOnApiJoin(this.merId, obj.id)
					.then(res => {
						this.Tips({ title: res.message, icon: 'success' });
						this.shopList.splice(index, 1);
					})
					.catch(err => {
						return this.Tips({
							title: err
						});
					});
			}



		},
		handleMethod(item, obj, index) {
			if (item.id == 1) {
				this.upperShelfAndLowerShelf(obj, index);
				return;
			}
			if (item.id == 2) {
				this.addToRecycleBin(obj, index);
				return;
			}
		}
	}
};
</script>

<style lang="scss" scoped>
@import './scss/index.scss';
.longTab {
	display: flex;
	width: 100%;
	/* #ifdef H5 */
	padding-bottom: 20rpx;
	/* #endif */
	/* #ifdef MP */
	padding-top: 12rpx;
	padding-bottom: 12rpx;
	/* #endif */
	.longItem {
		height: 50upx;
		display: inline-block;
		line-height: 50upx;
		text-align: center;
		font-size: 30rpx;
		color: #282828;
		max-width: 160rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		overflow-x: scroll;
		overflow-y: hidden;
		/*解决ios上滑动不流畅*/
		-webkit-overflow-scrolling: touch;

		&.click {
			font-weight: bold;
			font-size: 30rpx;
			color: #e93323;
			.underline {
				opacity: 1;
			}
		}
	}

	.underlineBox {
		height: 3px;
		width: 20%;
		display: flex;
		align-content: center;
		justify-content: center;
		transition: 0.5s;

		.underline {
			width: 60rpx;
			height: 4rpx;
			background-color: #e93323;
		}
	}
}
.title_search {
	background: #f5f5f5;
	border-radius: 30rpx;
	height: 60rpx;
	padding-left: 60rpx;
	position: relative;
	.iconfont {
		position: absolute;
		top: 50%;
		margin-top: -14rpx;
		left: 30rpx;
		font-size: 28rpx;
	}
	input {
		height: 60rpx;
		font-size: 26rpx;
		margin-left: 20rpx;
	}
}
.shop_button {
	margin-left: 18rpx;
	padding: 0 34rpx;
	height: 60rpx;
	border: 1px solid #c7c7c7;
	border-radius: 30px;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 30rpx;
	font-size: 26rpx;
	color: #999999;
}
.shop_list {
	margin-top: 20rpx;
	padding: 0 20rpx;

	&_item {
		padding: 30rpx;
		background: #ffffff;
		border-radius: 10px;
		margin-bottom: 20rpx;
		&_shop {
			display: flex;
			&_image,
			uni-image {
				width: 150rpx;
				height: 150rpx;
				border-radius: 16rpx;
				position: relative;
				image {
					width: 150rpx;
					height: 150rpx;
				}
			}
			position: relative;
			.spec {
				color: #ffffff;
				background: rgba(0, 0, 0, 0.5);
				border-radius: 16rpx 0 16rpx 0;
				text-align: center;
				font-size: 18rpx;
				display: inline-block;
				width: 84rpx;
				line-height: 30rpx;
				position: absolute;
				bottom: 0;
				right: 0;
			}
			&_con {
				flex: 1;
				display: flex;
				flex-direction: column;
				margin-left: 20rpx;
				padding-bottom: 12rpx;

				&_title {
					font-size: 28rpx;
					color: #282828;
					max-width: 500rpx;
					padding-top: 4rpx;
				}
				&_message {
					margin-top: 15rpx;
					font-size: 22rpx;
					color: #868686;
					> span:nth-child(1) {
						display: inline-block;
						margin-right: 20rpx;
					}
				}

				&_price {
					margin-top: 10rpx;
					> span {
						display: inline-block;
						margin-right: 7rpx;
						font-size: 30rpx;
						color: #e93323;
					}
					> del {
						color: #bebebe;
						font-size: 26rpx;
					}
				}
			}
		}
		&_handle {
			display: flex;
			justify-content: flex-end;
			> view {
				margin-left: 18rpx;
				padding: 0 34rpx;
				height: 60rpx;
				border: 1px solid #c7c7c7;
				border-radius: 30px;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 30rpx;
				font-size: 26rpx;
				color: #999999;
			}
		}
	}
}
</style>
